package com.chendx.store.goods.util;

import com.alibaba.fastjson.JSON;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * Created by chendx on 2022/5/23.
 */
public class GoodsUtils {
    public static String list2Spec(List<String> list){
        List<Map> mapList = new ArrayList<>();
        for (String mstr : list){
            Map map = JSON.parseObject(mstr, Map.class);
            mapList.add(map);
        }
        Set<String> keys = new HashSet<>();
        for (Map map : mapList){
            Set set = map.keySet();
            for (Object key : set){
                keys.add(String.valueOf(key));
            }
        }
        Map<String,List<String>> mm = new HashMap<>();
        for (String key : keys){
            List<String> values = new ArrayList<>();
            for (Map map : mapList){
                if (!StringUtils.isEmpty(map.get(key))){
                    values.add(String.valueOf(map.get(key)));
                }
            }
            mm.put(key,values);
        }
        return JSON.toJSONString(mm);
    }
}
